//
//  SCPhotoBrowserDismissedAnimation.swift
//  GZWeibo007
//
//  Created by newuser on 16/1/2.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit

class SCPhotoBrowserDismissedAnimation: NSObject, UIViewControllerAnimatedTransitioning {
    
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.25
    }
    
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        
        //获取控制器
        let formVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey) as! SCPhotoBrowserController
        
        //获取view
        let formView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
       
        //获取过渡视图
        let tempImageView = formVC.dismissTempImageView()
        
        //添加容器视图
        transitionContext.containerView()?.addSubview(tempImageView)
        
        //隐藏collection
        formVC.collectionView.hidden = true
        
        
        //弹出动画
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
               formView.alpha = 0
                tempImageView.frame = formVC.dismissTargetRect()
            
            }) { (_) -> Void in
                transitionContext.completeTransition(true)
        }
        
    }

}
